'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    // 创建电话黑名单表
    await queryInterface.createTable('phone_blacklist', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      phoneNumber: {
        type: Sequelize.STRING(30),
        allowNull: false,
        comment: '被拦截的电话号码'
      },
      reason: {
        type: Sequelize.STRING(255),
        allowNull: true,
        comment: '拦截原因'
      },
      status: {
        type: Sequelize.ENUM('active', 'inactive'),
        allowNull: false,
        defaultValue: 'active',
        comment: '拦截状态'
      },
      createdBy: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '创建者'
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });

    // 添加索引
    await queryInterface.addIndex('phone_blacklist', ['phoneNumber'], {
      unique: true,
      name: 'phone_blacklist_phone_unique'
    });

    // 创建内容黑名单表
    await queryInterface.createTable('content_blacklist', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      content: {
        type: Sequelize.STRING(255),
        allowNull: false,
        comment: '被拦截的内容（关键词或正则表达式）'
      },
      type: {
        type: Sequelize.ENUM('keyword', 'regex'),
        allowNull: false,
        defaultValue: 'keyword',
        comment: '内容类型：关键词或正则表达式'
      },
      action: {
        type: Sequelize.ENUM('block', 'flag'),
        allowNull: false,
        defaultValue: 'flag',
        comment: '动作：完全拦截或标记为敏感'
      },
      reason: {
        type: Sequelize.STRING(255),
        allowNull: true,
        comment: '拦截原因'
      },
      status: {
        type: Sequelize.ENUM('active', 'inactive'),
        allowNull: false,
        defaultValue: 'active',
        comment: '拦截状态'
      },
      createdBy: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '创建者'
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });

    // 添加复合索引
    await queryInterface.addIndex('content_blacklist', ['content', 'type'], {
      unique: true,
      name: 'content_blacklist_content_type_unique'
    });
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('phone_blacklist');
    await queryInterface.dropTable('content_blacklist');
  }
}; 